using System;
using System.Text;
using System.Reflection;



namespace Open.Data.Persistence
{
	public class Order: Column, ISort
	{
		//Property variables
        private SortDirection _direction;

        //Constructors
		internal protected Order(Type type, string field): base(type, field)
		{
			Direction = SortDirection.Ascending;
		}

        internal protected Order(Type type, string field, SortDirection order): base(type, field)
		{
			Direction = order;
		}

        //Properties
		public virtual SortDirection Direction
		{
			get
			{
				return _direction;
			}
			set
			{
				_direction = value;
			}
		}

        //Methods
        public override void AppendHashCode(StringBuilder builder)
        {
            PropertyInfo prop = PropertyInfo;
            
            if (typeof(IView).IsAssignableFrom(prop.ReflectedType))
            {
                CacheItem cache = Provider.Instance.ReflectionCache.Lookup(prop.ReflectedType);
                prop = cache.GetOriginalProperty(prop);
            }

            builder.Append(prop.ToString());
            builder.Append(_direction.ToString());
        }
	}
}
